package com
{
	import com.data.DragData;
	
	import flash.display.Sprite;

	/**
	 * 拖动类 
	 * @author pethan
	 * 
	 */	
	public class DragControl
	{
		private static var _instance:DragControl;
		
		public var drags:Vector.<DragData>;
		//拖动完成添加舞台上
		public var stage:Sprite;
		public  static function getInstance():DragControl{
			if(_instance==null){
				_instance=new DragControl(new SingleStance());
			}
			return _instance;
		}
		
		public function DragControl(se:SingleStance)
		{
			if(se==null){
				throw new Error('请使用单例');
			}
			drags=new Vector.<DragData>();
		}
		
		public function addDrag(dragTarget:Sprite,_hitTest:Sprite,needStage:Boolean=false,_stage:Sprite=null):void{
			for(var i:int=0;i<drags.length;i++){
				if(drags[i].dragSprite==dragTarget){
					break;
				}
			}
			if(i==drags.length){
				var dragData:DragData;
				if(needStage){
					 dragData=new DragData(dragTarget,_hitTest,this.stage);
				}else{
					 dragData=new DragData(dragTarget,_hitTest,_stage);
				}
				drags.push(dragData);
			}
		}
		
		public function removeDrag(dragTarget:Sprite):void{
			for(var i:int=0;i<drags.length;i++){
				if(drags[i].dragSprite==dragTarget){
					drags[i].clear();
					//ArrayHelp.deleteArray(drags,i);
				}
			}
		}
		
		/**
		 * 还原为初始状态 
		 * @param dragTarget
		 * 
		 */		
		public function toBack(dragTarget:Sprite):void{
			for(var i:int=0;i<drags.length;i++){
				if(drags[i].dragSprite==dragTarget){
					drags[i].toBack();
				}
			}
		}
	}
}

class SingleStance{}